package user

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserapiRegisterLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserapiRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserapiRegisterLogic {
	return &UserapiRegisterLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserapiRegisterLogic) UserapiRegister(req *types.UserRegisterReq) (resp *types.UserRegisterRes, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.Register(ctx, &usercenter.RegisterReq{
		Name:     req.Name,
		Password: req.Password,
		Phone:    req.Phone,
		Email:    req.Email,
		OpenId:   req.OpenId,
		Country:  req.Country,
		Gender:   req.Gender,
		Activity: req.Activity,
	})
	if err != nil {
		return nil, err
	}
	return &types.UserRegisterRes{
		Access:  res.Access,
		Refresh: res.Refresh,
	}, nil
}
